﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ETRobocon.RaceTraking.GameManagement
{
    /// <summary>
    /// RaceMgmtConsole.xaml の相互作用ロジック
    /// </summary>
    public partial class RaceMgmtConsole : UserControl
    {
        private ERRound _currentRound;
        public ERRound CurrentRound
        {
            get
            {
                return _currentRound;
            }
        }
        public RaceMgmtConsole()
        {
            InitializeComponent();

//            WSRaceMgmt.RaceMgmt rm = new WSRaceMgmt.RaceMgmt();
            var rm = new ETRoboConTrakingSystem.ETRoboConRM.ETRoboConRaceMgmtClient();

            ListContest lc = MainDoc.Resources["ContestList"] as ListContest;
            lc.UpdateContents(rm);
            rm.Close();
        }

        private void buttonStartRound_Click(object sender, RoutedEventArgs e)
        {
            var rm = new ETRoboConRM.ETRoboConRaceMgmtClient();
//            using (WSRaceMgmt.RaceMgmt rm = new WSRaceMgmt.RaceMgmt())
            {
                ERRound round = treeViewContests.SelectedItem as ERRound;
                if (round != null)
                {
                    int roundId;
                    if (rm.StartRound(out roundId, round.Parent.ID,round.RoundNo))
                    {
                        round.ID = roundId;
                        _currentRound = round;
                        updateRoundInfoStackPanel(round, "StartRound");
//                        stackPanelCurrentRound.Background = ;
                    }
                }
            }
            rm.Close();
        }

        private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
        {
            if (treeViewContests.SelectedItem != null && treeViewContests.SelectedItem is ERRound)
            {
                ERRound round = treeViewContests.SelectedItem as ERRound;
                var rm = new ETRoboConRM.ETRoboConRaceMgmtClient();
//                using (WSRaceMgmt.RaceMgmt rm = new WSRaceMgmt.RaceMgmt())
                {
                    int roundId;
                    int roundNo;
                    if (rm.GetCurrentRound(out roundNo,out roundId, round.Parent.ID))
                    {
                        round.ID = roundId;
                        _currentRound = round;
                        round.RoundNo = roundNo;
                        updateRoundInfoStackPanel(round, "Current Round");
                    }
                }
                rm.Close();

            }
        }

        private void updateRoundInfoStackPanel(ERRound round, string status)
        {
            textBoxStatus.Text = status;
            textBoxContest.Text = "Contest:=" + round.Parent.Parent.Name;
            textBoxGame.Text = "Game:=" + round.Parent.Name;
            textBoxRound.Text = "RoundID:=" + round.ID.ToString();
            textBoxRound.Text = "Round No:=" + round.RoundNo.ToString();
            stackPanelCurrentRound.Opacity = 1;
        }
    }

 }
